//
//  AlertTool.swift
//  AudioToolProject
//
//  Created by 邹琳 on 2019/5/5.
//  Copyright © 2019 邹琳. All rights reserved.
//

import Foundation
import SVProgressHUD

class AlertTool: NSObject {
    
    /** 选项卡 */
    static func createSheetAlertWithTitle(_ title:String,actionTitles:[String],finish:((Int)->Void)?){
        let alertVc = UIAlertController(title: title, message: nil, preferredStyle: .actionSheet)
        if actionTitles.count == 0 {
            return
        }
        
        for (idx,actionTitle) in actionTitles.enumerated(){
            let action = UIAlertAction(title: actionTitle, style: .default) { (action) in
                if finish != nil{
                    finish!(idx)
                }
            }
            alertVc.addAction(action)
        }
        
        let cancelAction = UIAlertAction(title: "取消", style: .cancel, handler: nil)
        alertVc.addAction(cancelAction)
        
        self.alert(alertVc)
    }
    
    /** 确定取消选择提示 */
    static func createSelect(_ title:String,detail:String,finish:(()->Void)?){
        let alertVc = UIAlertController(title: title, message: detail, preferredStyle: .alert)
        let sureAction = UIAlertAction(title: "确定", style: .default) { (action) in
            if finish != nil{
                finish!()
            }
        }
        let cancelAction = UIAlertAction(title: "取消", style: .cancel, handler: nil)
        
        alertVc.addAction(sureAction)
        alertVc.addAction(cancelAction)
        
        self.alert(alertVc)
    }
    
    /** 消息提示 */
    static func create(_ title:String,detail:String){
        let alertVc = UIAlertController(title: title, message: detail, preferredStyle: .alert)
        let sureAction = UIAlertAction(title: "确定", style: .cancel)
        alertVc.addAction(sureAction)
        
        self.alert(alertVc)
    }
    
    /** 显示提醒 */
    private static func alert(_ controller:UIViewController){
        if UIApplication.shared.keyWindow?.rootViewController?.presentationController != nil{
            UIApplication.shared.keyWindow?.rootViewController!.present(controller, animated: true, completion: nil)
        }else{
            UIApplication.shared.keyWindow?.rootViewController?.present(controller, animated: true, completion: nil)
        }
    }
}

extension AlertTool{
    static func alertSuccess(_ msg:String){
        SVProgressHUD.alertSuccess(msg)
    }
    
    static func alertInfo(_ msg:String){
        SVProgressHUD.alertInfo(msg)
    }
    
    static func alertError(_ msg:String){
        SVProgressHUD.alertError(msg)
    }
    
    static func alertState(_ msg:String){
        SVProgressHUD.alertState(msg)
    }
}

extension SVProgressHUD{
    static func alertSuccess(_ msg:String){
        SVProgressHUD.setDefaultStyle(.dark)
        SVProgressHUD.setMinimumDismissTimeInterval(1.5)
        SVProgressHUD.setMaximumDismissTimeInterval(5.0)
        SVProgressHUD.showSuccess(withStatus: msg.zlLocalizedString)
    }
    
   static func alertInfo(_ msg:String){
        SVProgressHUD.setDefaultStyle(.dark)
        SVProgressHUD.setMinimumDismissTimeInterval(1.5)
        SVProgressHUD.setMaximumDismissTimeInterval(5.0)
        SVProgressHUD.showInfo(withStatus: msg.zlLocalizedString)
    }
    
   static func alertError(_ msg:String){
        SVProgressHUD.setDefaultStyle(.dark)
        SVProgressHUD.setMinimumDismissTimeInterval(1.5)
        SVProgressHUD.setMaximumDismissTimeInterval(5.0)
        SVProgressHUD.showError(withStatus: msg.zlLocalizedString)
    }
    
   static func alertState(_ msg:String){
        SVProgressHUD.setDefaultStyle(.dark)
        SVProgressHUD.setMaximumDismissTimeInterval(15.0)
        SVProgressHUD.show(withStatus: msg.zlLocalizedString)
    }
}
